/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-dealer
*
* @File name : DealerController.java
*
* @Author : Administrator
*
* @Date : 2017年12月5日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年12月5日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.dealer.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import org.marketing.common.Response;
import com.yonyou.gmmc.service.dealer.constant.DealerConstant;
import com.yonyou.gmmc.service.dealer.entity.UserInfoPO;
import com.yonyou.gmmc.service.dealer.model.TmUsers;
import com.yonyou.gmmc.service.dealer.service.DealerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator 
 * 企业号服务API
 * @date 2017年12月5日
 */
@RestController
@RequestMapping(value = DealerConstant.BASE_PATH + "/" + DealerConstant.VERSION)
@Api("企业号服务API")
public class DealerController {

    private Logger logger = Logger.getLogger(DealerController.class);
    
    @Autowired
    private DealerService dealerService;

    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "测试接口", notes = "测试接口")
    public Response<String> test() throws Exception {
        try {
            return new Response<String>(ResultMsgConstants.RESULT_CODE_SUCCESS, "调用微服务成功", "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    @RequestMapping(value = "/userInfo", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取人员信息", notes = "获取人员信息")
    public Response<List<TmUsers>> userInfo(@RequestParam("userId") String userId,
                                            @RequestParam("phone") String phone,
                                            @RequestParam(required = false, defaultValue = "") String name,
                                            @RequestParam(required = false, defaultValue = "") String avatar) throws Exception {
        List<TmUsers> resultList = new ArrayList<TmUsers>();
        try{
            resultList = dealerService.userInfo(userId,phone,name,avatar);
            if(resultList.size()==0){
                return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, "获取人员信息异常，请联系管理员校验！");
            }else{
                return new Response<List<TmUsers>>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultList, "");
            }
        } catch (Exception e) {
            logger.error("获取人员信息发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    @RequestMapping(value = "/users", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "同步人员信息", notes = "同步人员信息")
    public Response<Map<String,Object>> users(@RequestBody UserInfoPO userInfo) throws Exception{
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            if(!CheckUtil.NullOrEmpty(userInfo.getStatus())&&userInfo.getStatus().equals("1")){
                logger.info("该成员状态为已激活");
                result = dealerService.users(userInfo);
                return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
            }else{
                return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, "该成员激活状态为已禁用或未激活");
            }
        } catch (Exception e) {
            logger.error("同步信息发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
}
